// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const studentDB = cloud.database().collection("student")
// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.op) {
    case "add":
      return studentDB.add({
        data: {
          u_id: event.u_id,
          i_id: event.i_id,
          name: event.name,
          sex: event.sex,
          height: event.height,
          weight: event.weight,
          grade: event.grade,
          class: event.class,
          special: event.special,
          bust: event.bust,
          waist: event.waist,
          hip: event.hip,
          default: event.default
        }
      }).then(res => { return res }).catch(console.error)
    case "delete":
      return studentDB.doc(event.id).remove().then(res => { return res }).catch(console.error)
    case "update":
      return studentDB.doc(event.id).update({
        data: {
          u_id: event.u_id,
          i_id: event.i_id,
          name: event.name,
          sex: event.sex,
          height: event.height,
          weight: event.weight,
          grade: event.grade,
          class: event.class,
          special: event.special,
          bust: event.bust,
          waist: event.waist,
          hip: event.hip,
          default: event.default
        }
      }).then(res => { return res }).catch(console.error)
    case "select":
      switch (event.key) {
        case "_id":
          return studentDB.where({
            _id: event.value
          }).get().then(res => { return res }).catch(console.error)
        case "u_id":
          return studentDB.where({
            u_id: event.value
          }).get().then(res => { return res }).catch(console.error)
        case "i_id":
          return studentDB.where({
            i_id: event.value
          }).get().then(res => { return res }).catch(console.error)
        case "all":
          return studentDB.get().then(res => { return res; }).catch(console.error)
      }
  }
}